package com.pcbly.web.components.utils.fetion;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

@SuppressWarnings("unchecked")
public class FriendUtil {

	List<FetionFriend> friends = new ArrayList<FetionFriend>(); // 从xml解析得到
	// 的所有好友
	List<FriendGroup> groups = new ArrayList<FriendGroup>(); // 好友分组信息

	
	public void getFriendsFromXml(String xml) throws Exception {

		SAXReader sr = new SAXReader();
		Document document = sr.read(new ByteArrayInputStream(xml.getBytes()));

		// 获取根元素
		Element root = document.getRootElement();

		// 获取所有的好友组
		Element buddyListElement = root.element("buddy-lists");// 迭代xml文件中“buddy-lists”元素

		// 迭代子元素
		for (Iterator<Element> bitrator = buddyListElement
				.elementIterator("buddy-list"); bitrator.hasNext();) {

			FriendGroup group = new FriendGroup();

			Element fieldElement = bitrator.next();// 迭代xml文件中“field”元素

			group.setGroupId(fieldElement.attributeValue("id"));
			group.setGroupName(fieldElement.attributeValue("name"));// 设置属性参数

			groups.add(group);

		}

		// 获取所有的好友
		Element friendElement = root.element("buddies");// 迭代xml文件中“buddy-lists”元素

		// 迭代子元素
		for (Iterator<Element> bitrator = friendElement
				.elementIterator("buddy"); bitrator.hasNext();) {

			FetionFriend friend = new FetionFriend();

			Element fieldElement = bitrator.next();// 迭代xml文件中“field”元素

			friend.setName(fieldElement.attributeValue("local-name"));
			friend.setNumber("");
			friend.setUserId(fieldElement.attributeValue("user-id"));
			friend.setUri(fieldElement.attributeValue("uri"));
			friend.setGroup(getGroupById(groups, fieldElement
					.attributeValue("buddy-lists")));

			friends.add(friend);

		}

		// 获取所有的手机好友（不是飞信好友）
		// 获取所有的好友
		Element moElement = root.element("mobile-buddies");// 迭代xml文件中“buddy-lists”元素

		// 迭代子元素
		for (Iterator<Element> bitrator = moElement
				.elementIterator("mobile-buddy"); bitrator.hasNext();) {

			FetionFriend friend = new FetionFriend();

			Element fieldElement = bitrator.next();// 迭代xml文件中“field”元素

			friend.setName(fieldElement.attributeValue("local-name"));
			friend.setNumber(fieldElement.attributeValue("uri").split(":")[1]);
			friend.setUserId(fieldElement.attributeValue("user-id"));
			friend.setUri(fieldElement.attributeValue("uri"));
			friend.setGroup(getGroupById(groups, fieldElement
					.attributeValue("buddy-lists")));

			friends.add(friend);

		}

	}

	/**
	 * 得到所有的好友信息 包括电话号码
	 * 
	 * @param xml
	 * @throws Exception
	 */
	public List<FetionFriend> getDetailFriendsFromXml(
			List<FetionFriend> fromfriends, String xml) throws Exception {

		SAXReader sr = new SAXReader();
		Document document = sr.read(new ByteArrayInputStream(xml.getBytes()));
		List<FetionFriend> tofriends = new ArrayList<FetionFriend>();

		// 获取根元素
		Element root = document.getRootElement();

		// 迭代子元素
		for (Iterator<Element> bitrator = root.elementIterator("contact"); bitrator
				.hasNext();) {
			Element e = bitrator.next();// 迭代xml文件中“contact”元素
			// 获取URI
			String uri = e.attributeValue("uri");

			FetionFriend f = getFriendByUri(fromfriends, uri);

			// 获取好友的详细信息
			Element e2 = e.element("personal");
			if (f != null) {
				f.setNickName(e2.attributeValue("nickname"));
				f.setNumber(e2.attributeValue("mobile-no"));
				tofriends.add(f);
			}

		}
		// 添加手机好友：不是飞信好友,URI是这样的形式：tel:13475927282
		for (FetionFriend f : fromfriends) {
			if (f.getUri().split(":")[0].equals("tel")) {
				f.setNumber(f.getUri().split(":")[1]);
				tofriends.add(f);
			}
			
		}



		return tofriends;

	}

	public FriendGroup getGroupById(List<FriendGroup> groups, String id) {

		String toId = id;
		// 如果好友在多个分组之中,简单的放在第一个分组中
		if (id.split(";").length > 1) {
			toId = id.split(";")[0];
		}
		for (FriendGroup g : groups) {

			if (g.getGroupId().equals(toId))
				return g;

		}

		return null;
	}

	public FetionFriend getFriendByUri(List<FetionFriend> friends, String uri) {

		for (FetionFriend f : friends) {

			if (f.getUri().equals(uri))
				return f;

		}
		return null;
	}

}
